/**
 *  Stream Recorder: Records RealPlayer(tm) streams as MP3s
 *  Copyright (C) 2007  David Griffiths
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package dg.radio2ipod.controller;

import com.apple.eawt.Application;
import com.apple.eawt.ApplicationEvent;
import com.apple.eawt.ApplicationListener;
import java.io.File;
import dg.radio2ipod.Main;
import dg.radio2ipod.model.StreamFactory;
import java.net.URL;


/**
 *
 * @author davidg
 */
public final class MacAppListener implements ApplicationListener, URLListener {
    static {
        Application application = Application.getApplication();
        MacAppListener listener = new MacAppListener();
        application.addApplicationListener(listener);
        application.setEnabledPreferencesMenu(true);
        GURLHandler.getInstance().register(listener);
        GURLHandler.getInstance().enable();
    }
    
    
    public MacAppListener() {
    }
    
    public void handleAbout(final ApplicationEvent event)  {
        Main.showAbout();
        event.setHandled(true);
    }
    
    public void handleOpenApplication(final ApplicationEvent event) {
        if (event.getFilename() != null) {
            handleOpenFile(event);
        }
    }
    
    public void handleOpenFile(final ApplicationEvent event) {
        try {
            Main.addStream(StreamFactory.getStream(new File(
                    event.getFilename())));
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
    
    public void handlePreferences(final ApplicationEvent event) {
        Main.showPreferences();
        event.setHandled(true);
    }
    
    public void handlePrintFile(final ApplicationEvent event) {
    }
    
    public void handleQuit(final ApplicationEvent event) {
        Main.handleQuit();
        event.setHandled(true);
    }
    
    public void handleReOpenApplication(final ApplicationEvent event) {
    }
    public void handleOpenURL(final String url) {
        try {
            Main.addStream(StreamFactory.getStream(new URL(url)));
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
}
